package com.shu.consumer_producer;

import lombok.extern.slf4j.Slf4j;

import java.security.PublicKey;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/8/25 16:49
 */
@Slf4j
public class Test1 {
    public static void main(String[] args) {
        AtomicInteger x = new AtomicInteger();

        LongAdder y = new LongAdder();

        BlockingQueue<Object> queue = new ArrayBlockingQueue<>(10);

        Runnable producer = () -> {
            while (true) {
                try {
                    queue.put(new Object());
//                    log.info("put:" + x.incrementAndGet());
                    y.increment();
                    log.info("put:" + y.intValue());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        new Thread(producer).start();
        new Thread(producer).start();

        Runnable consumer = () -> {
            while (true) {
                try {
                    queue.take();
//                    log.info("take:" + x.get());
                    log.info("take:" + y.intValue());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        new Thread(consumer).start();
        new Thread(consumer).start();
    }
}
